﻿using System;
using Xtee.XteeAdapter.Colors;
using System.Linq;
using Colors.Xroad.Eu;
using Colors.Xroad.Eu.Headers;
using Colors.Xroad.Eu.Messages;
using Xtee.Core.Envelope.Header;


namespace Server
{
    public class MixSingleService : MixSingleServiceHandler
    {
        public MixSingleResponse ProcessRequest(MixSingle input, MixSingleInputHeader header)
        {
            return Blend(input.Mix.One, input.Mix.Other);
        }

        public static MixSingleResponse Blend(Color a, Color b)
        {
            short red = a.First();
            short green = a.Skip(1).First();
            short blue = a.Last();

            short b_red = b.First();
            short b_green = b.Skip(1).First();
            short b_blue = b.Last();

            int _r = Math.Min((red + b_red), 255);
            int _g = Math.Min((green + b_green), 255);
            int _b = Math.Min((blue + b_blue), 255);

            var result = new MixSingleResponse { Color = { Convert.ToByte(_r), Convert.ToByte(_g), Convert.ToByte(_b) } };
            return result;
        }
    }
}